#!/usr/bin/perl
#setpass-lib.pl
#Library for editing webmin users, password and access rights

do '../web-lib.pl';

&init_config();
%access = &get_module_acl();

#get_telnet_status(void)
#return $rt_status,   if telnet is started, $rt_status = 'started', else telnet is stopped , $tr_status='stopped'
sub get_telnet_status()
{
	my (@file, $i, @line, $rt, @out, $xinet_stat);

	$xinet_stat = 0;

	@out = `ps -A|grep 'xinetd'`;


	for($i = 0; $i < @out; $i++)
	{
		if($out[$i] =~ /xinetd/)
		{
			$xinet_stat = 1;
			last;
		}
	}

	return 'stopped' if($xinet_stat == 0);

	open(TELNETCONF, '/etc/xinetd.d/telnet');
	@file = <TELNETCONF>;
	close(TELNETCONF);

	$rt = 'warning';

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);

		@line = split('=', $file[$i]);
		$line[0] =~ s/\s+//g;

		if($line[0] eq 'disable')
		{
			$line[1] =~ s/\s+//g;
			if($line[1] eq 'yes')
			{
				$rt = 'stopped';
				last;
			}else{
				$rt = 'started';
				last;				
			}
		}
	}

	return $rt;
}

#operate_telnetserv($oper)
#oper is operation for telnet service
sub operate_telnetserv()
{
	my (@file, $i, @line, $oper, $strline, $disable, @out, $xinet_stat);

	$oper = @_[0];

	if($oper eq 'start') {
		$disable = 'no';
	}elsif($oper eq 'stop')	{
		$disable = 'yes';
	}else {
		return "no action";
	}

	open(TELNETCONF, '/etc/xinetd.d/telnet');
	@file = <TELNETCONF>;
	close(TELNETCONF);

	for($i = 0; $i < @file; $i++)
	{
		$strline = $file[$i];
		chomp($strline);
		$strline =~ s/#.*$//;

		@line = split('=', $strline);
		$line[0] =~ s/\s+//g;

		if($line[0] eq 'disable')
		{
			$file[$i] = "\t" . "disable" . "\t\t" . "= " . $disable . "\n";
			last;
		}
	}

	

	open(TELNETCONF, '>/etc/xinetd.d/telnet');
	print TELNETCONF @file;
	close(TELNETCONF);

	@out = `ps -A|grep 'xinetd'`;
	$xinet_stat = 0;

	for($i = 0; $i < @out; $i++)
	{
		if($out[$i] =~ /xinetd/)
		{
			$xinet_stat = 1;
			last;
		}
	}

	if($xinet_stat)
	{
		`/etc/init.d/xinetd reload`;
	}
	else
	{
		`/etc/init.d/xinetd restart`;
	}
}


1;
